#include <vector>
using namespace std;

int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
  int n = gas.size();
  vector<int> remain_gas(n, 0);
  for (int i = 0; i < n; ++i) {
    remain_gas[i] = gas[i] - cost[i];
  }
  int left = 0;
  while (left < n) {
    int cur_gas = 0;
    bool ok = true;
    for (int i = 0; i < n; ++i) {
      cur_gas += remain_gas[(left + i) % n];
      if (cur_gas < 0) {
        left = left + i + 1;
        ok = false;
        break;
      }
    }
    if (ok) {
      return left;
    }
  }
  return -1;
}